/****************************************************************************
**
** Copyright (C) 2010 Tieto Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Tieto Corporation (info.ostrava@tieto.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Tieto.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Tieto gives you certain additional
** rights.  These rights are described in the Tieto Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Tieto at info.ostrava@tieto.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef IMAGE_H
#define IMAGE_H

#include <QtGui/QImage>
#include <QtCore/QMetaType>

/**
 * Color image
 */
class ColorImage : public QImage
{    

public:

    /**
     * Default constructor
     */
    ColorImage();

    /**
     * Constructor
     * @param size image size
     */
    ColorImage(const QSize & size);

    /**
     * Constructor
     * @param width image width
     * @param height image height
     */
    ColorImage(int width, int height);

    ColorImage &operator=(const QImage &image);
};

Q_DECLARE_METATYPE(ColorImage)

/**
 * Grayscale
 */
typedef unsigned char Grayscale;

/**
 * Grayscale image
 * The image pixels are represented in format 0x000000GG
 * The 0x000000GG format: G - gray
 */
class GrayscaleImage : public ColorImage
{
public:

    /**
     * Default constructor
     */
    GrayscaleImage();

    /**
     * Constructor
     * @param size image size
     */
    GrayscaleImage(const QSize & size);

    /**
     * Constructor
     * @param width image width
     * @param height image height
     */
    GrayscaleImage(int width, int height);

    /**
     * Gets color of pixel
     * @return grayscale color of pixel
     */
    Grayscale grayscale(int x, int y) const;

    /**
     * Sets color of pixel
     * @param grayscale color of pixel
     */
    void setGrayscale(int x, int y, Grayscale gray);

    GrayscaleImage &operator=(const QImage &image);

private:

    QRgb pixel(int x, int y) const;
    QRgb pixel(const QPoint &pt) const;
    void setPixel(int x, int y, uint index_or_rgb);
    void setPixel(const QPoint &pt, uint index_or_rgb);
};

Q_DECLARE_METATYPE(GrayscaleImage)

/**
 * Binary colors
 */
enum BinaryColor {
    Black = 0,
    White
};

/**
 * Binary image
 * The image pixel are represented in format 0x0000000I
 * The 0x0000000I format: I - binary color
 */
class BinaryImage : public ColorImage
{
public:

    /**
     * Default constructor
     */
    BinaryImage();

    /**
     * Constructor
     * @param size image size
     */
    BinaryImage(const QSize & size);

    /**
     * Constructor
     * @param width image width
     * @param height image height
     */
    BinaryImage(int width, int height);

    /**
     * Gets binary color of pixel
     * @return binary color of pixel
     */
    BinaryColor binaryColor(int x, int y) const;

    /**
     * Sets color of pixel
     * @param color binary color of pixel
     */
    void setBinaryColor(int x, int y, BinaryColor color);

    BinaryImage &operator=(const QImage &image);

private:

    QRgb pixel(int x, int y) const;
    QRgb pixel(const QPoint &pt) const;
    void setPixel(int x, int y, uint index_or_rgb);
    void setPixel(const QPoint &pt, uint index_or_rgb);
};

Q_DECLARE_METATYPE(BinaryImage);

/**
 * Indexed image
 * The image pixel are represented in format 0x00IIIIII
 * The 0x00IIIIII format: I - index color
 */
class IndexImage : public ColorImage
{
public:

    /**
     * Default constructor
     */
    IndexImage();

    /**
     * Constructor
     * @param size image size
     */
    IndexImage(const QSize & size);

    /**
     * Constructor
     * @param width image width
     * @param height image height
     */
    IndexImage(int width, int height);

    /**
     * Gets index stored in pixel
     * @return index stored in pixel
     */
    uint index(int x, int y) const;

    /**
     * Sets index stored in pixel
     * @param index index stored in pixel
     */
    void setIndex(int x, int y, uint index);

    /**
     * Sets count of indexes
     * @param count count of indexes
     */
    void setIndexCount(const int count);

    /**
     * Gets count of indexes
     * @return count of indexes
     */
    int indexCount() const;

    IndexImage &operator=(const QImage &image);

private:

    QRgb pixel(int x, int y) const;
    QRgb pixel(const QPoint &pt) const;
    void setPixel(int x, int y, uint index_or_rgb);
    void setPixel(const QPoint &pt, uint index_or_rgb);

    /**
     * Count of indexes
     */
    int _indexCount;
};

Q_DECLARE_METATYPE(IndexImage);

/**
 * Indexed areas
 * Stores indexed areas data and its image representation
 */
struct IndexedArea
{
    /**
     * Indexed areas data
     */
    IndexImage data;

    /**
     * Indexed areas image representation
     */
    ColorImage image;
};

Q_DECLARE_METATYPE(IndexedArea);

#endif // IMAGE_H
